package com.wonders.bigdata.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author xb
 * @descripton: PDM解析
 * @date 2023/5/14
 */
public class PdmParser {
    public static void main(String[] args) {
        String file = "D:/file/文件解析/人口.pdm";
        long start = System.currentTimeMillis();
        try {
            System.out.println(parser(file));
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("总耗时：" + (System.currentTimeMillis() - start)/1000 + "秒");
    }

    private static JSONObject parser(String filePath) throws DocumentException {
        JSONObject obj = new JSONObject();
        SAXReader reader = new SAXReader();
        File file = new File(filePath);
        Document doc = reader.read(file);
        // 获取全部符合标签<c:Children>下的<o:Model>
        Node model = doc.selectSingleNode("//c:Children/o:Model");
        // 获取表信息
        getHeadInfo(model, obj);
        // 获取库信息
        getDbInfo(model, obj);
        // 获取表以及字段信息
        getTableAndCloumn(model, obj);
        // 获取附近参考信息
        getReferencesInfo(model, obj);
        // 获取目标模型信息
        getTargetModel(model, obj);
        return obj;
    }

    private static void getTargetModel(Node model, JSONObject obj) {
    }

    private static void getReferencesInfo(Node model, JSONObject obj) {
    }

    private static void getTableAndCloumn(Node model, JSONObject obj) {
        if (model.selectNodes("c:Tables/o:Table") == null) {
            return;
        }
        JSONArray array = new JSONArray();
        for (Object o : model.selectNodes("c:Tables/o:Table")) {
            JSONObject tableObj = new JSONObject();
            Node tableNode = (Node) o;
            tableObj.put("tableId", ((Element) tableNode).attributeValue("Id"));
            // 获取表的所有属性
            for (Object element : ((Element) tableNode).elements()) {
                // 标签名
                String name = ((Element) element).getQName().getName();
                String text = ((Element) element).getText();
                if (name.equalsIgnoreCase("CreationDate")) {
                    text = getDate(text);
                }else if (name.equalsIgnoreCase("ModificationDate")) {
                    text = getDate(text);
                }
                tableObj.put("table" + name, text);
                if (name.equalsIgnoreCase("TotalSavingCurrency")) {
                    break;
                }
            }
            // 键信息
            JSONArray keyArra = new JSONArray();
            for (int i=0;i<tableNode.selectNodes("c:Keys/o:Key").size();i++) {
                JSONObject key = new JSONObject();
                Node keyNode = (Node) tableNode.selectNodes("c:Keys/o:Key").get(i);
                key.put("tableKeyId", ((Element) keyNode).attributeValue("Id"));
                // 获取所有属性
                for (Object element : ((Element) keyNode).elements()) {
                    // 标签名
                    String name = ((Element) element).getQName().getName();
                    String text = ((Element) element).getText();
                    if (name.equalsIgnoreCase("CreationDate")) {
                        text = getDate(text);
                    }else if (name.equalsIgnoreCase("ModificationDate")) {
                        text = getDate(text);
                    }
                    key.put("tableKey" + name, text);
                }
                keyArra.add(key);
            }
            tableObj.put("tableKey", keyArra);
            // 字段信息
            JSONArray columnArra = new JSONArray();
            for (Object columnO : tableNode.selectNodes("c:Columns/o:Column")) {
                JSONObject column = new JSONObject();
                Node columnNode = (Node) columnO;
                column.put("columnId", ((Element) columnNode).attributeValue("Id"));
                // 获取所有属性
                for (Object element : ((Element) columnNode).elements()) {
                    // 标签名
                    String name = ((Element) element).getQName().getName();
                    String text = ((Element) element).getText();
                    if (name.equalsIgnoreCase("CreationDate")) {
                        text = getDate(text);
                    }else if (name.equalsIgnoreCase("ModificationDate")) {
                        text = getDate(text);
                    }
                    column.put("column" + name, text);
                }
                columnArra.add(column);
            }
            tableObj.put("columnList", columnArra);
            array.add(tableObj);
        }
        obj.put("table", array);
    }

    private static void getDbInfo(Node model, JSONObject obj) {
        Node dbMs = model.selectSingleNode("//o:Shortcut");
        obj.put("dbMsId", ((Element) dbMs).attributeValue("Id"));
        List elements = ((Element) dbMs).elements();
        for (Object element : elements) {
            // 标签名
            String name = ((Element) element).getQName().getName();
            String text = ((Element) element).getText();
            if (name.equalsIgnoreCase("CreationDate")) {
                text = getDate(text);
            }else if (name.equalsIgnoreCase("ModificationDate")) {
                text = getDate(text);
            }
            obj.put("dbMs" + name, text);
        }
    }

    private static void getHeadInfo(Node model, JSONObject obj) {
        obj.put("id", ((Element) model).attributeValue("Id"));
        obj.put("name", model.selectSingleNode("a:Name").getText());
        obj.put("code", model.selectSingleNode("a:Code").getText());
        obj.put("creationDate", getDate(model.selectSingleNode("a:CreationDate").getText()));
        obj.put("creator", model.selectSingleNode("a:Creator").getText());
        obj.put("modificationDate", getDate(model.selectSingleNode("a:ModificationDate").getText()));
        obj.put("modifier", model.selectSingleNode("a:Modifier").getText());
    }

    // 获取时间
    private static String getDate(String dateLong) {
        Long time = Long.parseLong(dateLong);
        //HH是24小时制，12小时用hh
        SimpleDateFormat formatTime= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date =new Date(time);
        return formatTime.format(date);
    }
}
